package com.jams.goodox.activity.serve.enterprise;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.adapter.CustomFragmentPagerAdapter;
import com.jams.goodox.entity.resp.LoanDetailsResp;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.EnterpriseService;
import com.jams.goodox.utils.permission.PhoneUtils;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;

import static com.jams.goodox.activity.serve.enterprise.BankLendActivity.LEND_PRODUCT_ID;


/**
 * 银行贷款详情
 */
public class BankLendDetailActivity extends BaseActivity {
    @Bind(R.id.sdv_bank_icon)
    SimpleDraweeView mSdvBankIcon;
    @Bind(R.id.tv_department_name)
    TextView mTvDepartmentName;
    @Bind(R.id.tv_tel)
    TextView mTvTel;
    @Bind(R.id.tv_department_address)
    TextView mTvDepartmentAddress;
    @Bind(R.id.tv_phone)
    TextView mTvPhone;
    @Bind(R.id.tabs_loan)
    TabLayout mTabsLoan;
    @Bind(R.id.vp_loan)
    ViewPager mVpLoan;
    

    /**
     * 网络请求时传入的 id
     */
    private String productId;
    /**
     * 标题
     */
    private List<String> tittleList = new ArrayList<>();
    /**
     * 显示的Fragment集合
     */
    private List<Fragment> contentList = new ArrayList<>();

    private String phone;
    private FragmentPagerAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_loan_detail;
    }

    @Override
    protected void init() {
        productId = StringUtils.newString(getIntent().getStringExtra(LEND_PRODUCT_ID));

        StyleUtil.titleBackKey(this, getResources().getString(R.string.loans_detail));
        getLendData();
    }

    @OnClick(R.id.tv_phone)
    public void onViewClicked() {
         new PhoneUtils(mContext).makeCall(StringUtils.newString(""), StringUtils.newString(phone));
    }


    /**
     * 获取贷款数据
     */
    private void getLendData() {
        showLoading();
        enqueue(createCallService(EnterpriseService.class).getLendProduct(productId), new RetrofitCallbackListener<LoanDetailsResp>() {
            @Override
            public void onSuccess(Call<LoanDetailsResp> call, LoanDetailsResp result) {
                misLoading();
                try {
                    mTvPhone.setVisibility(View.VISIBLE);
                    phone = result.data.connection;
                    mTvTel.setText("TEL:"+result.data.connection);
                    mSdvBankIcon.setImageURI(StringUtils.newString(result.data.icon));
                    mTvDepartmentName.setText(result.data.departmentName);
                    mTvDepartmentAddress.setText(result.data.address);
                    tittleList.add(getResources().getString(R.string.liquidity_loans));
                    tittleList.add(getResources().getString(R.string.mortgage_loans));
                    contentList.add(EnterprisePolicyDetailFragment.newInstance((StringUtils.newString(result.data.floatMoneyLend))));
                    contentList.add(EnterprisePolicyDetailFragment.newInstance((StringUtils.newString(result.data.mortgage))));
                    mAdapter = new CustomFragmentPagerAdapter(getSupportFragmentManager(), tittleList, contentList);
                    mVpLoan.setAdapter(mAdapter);
                    mTabsLoan.setupWithViewPager(mVpLoan);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(String msg) {
                misLoading();
            }
        });
    }
    
}
